﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Minestorm.Retro.GameLogic;

namespace Minestorm.Retro.Entities
{
    public class Star : Entity
    {
        private Texture2D _texture;
        private GraphicsDevice _graphics;
        private SpriteBatch _spriteBatch;

        public Star(GraphicsDevice graphics, Vector2 position, Vector2 finalPosition, TextureManager textureManager)
            : base(graphics, position)
        {
            _graphics = graphics;
            FinalPosition = finalPosition;
            Hidden = true;

            _texture = textureManager.GetTexture("star");
        }

        public Star(GraphicsDevice graphics, Vector2 position, TextureManager textureManager)
            : this(graphics, position, position, textureManager)
        { }

        public bool AutoSpawn { get; set; }

        public bool ShouldAutoSpawn(GameTime gameTime)
        {
            return AutoSpawn && gameTime.TotalGameTime - BornOn > TimeSpan.FromSeconds(2);
        }

        public bool Deploying { get; set; }

        public Vector2 FinalPosition { get; private set; }

        public override void Initialize()
        {
            var verticies = new VertexPositionTexture[4];
            verticies[0] = new VertexPositionTexture(new Vector3(-.075f, -.075f, 0), Vector2.Zero);
            verticies[1] = new VertexPositionTexture(new Vector3(.075f, -.075f, 0), new Vector2(_texture.Width, 0));
            verticies[2] = new VertexPositionTexture(new Vector3(.075f, .075f, 0), new Vector2(_texture.Width, _texture.Height));
            verticies[3] = new VertexPositionTexture(new Vector3(-.075f, .075f, 0), new Vector2(0, _texture.Height));

            SetVerticies(verticies);

            SetIndicies("star", false,
                0, 1, 2, 2, 3, 0
            );

            _spriteBatch = new SpriteBatch(_graphics);
        }

        public override void Draw(GameTime gameTime)
        {
            //DrawInternal(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
            var vec = _graphics.Viewport.Project(Vector3.Zero, Level.Projection, Level.View, GetWorld("star"));

            _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            _spriteBatch.Draw(_texture, new Rectangle((int)vec.X - (_texture.Width / 2), (int)vec.Y - (_texture.Height / 2), _texture.Width, _texture.Height), Constants.VectorColor);
            _spriteBatch.End();
        }

        public override Matrix GetWorld(string indexBufferName)
        {
            return Matrix.Identity * Matrix.CreateTranslation(Position.X, Position.Y, 0);
        }

        public override Texture2D GetTexture()
        {
            return _texture;
        }

        public override bool DrawVertices
        {
            get
            {
                return false;
            }
        }
    }
}
